﻿using System;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace EuphoricEngine3D.Helpers
{
    public static class MathsHelper
    {
        #region Trig Math

        public static float sin(float value)
        {
            return (float)Math.Sin(value);
        }

        public static float cos(float value)
        {
            return (float)Math.Cos(value);
        }

        public static float tan(float value)
        {
            return (float)Math.Tan(value);
        }

        public static float asin(float value)
        {
            return (float)Math.Asin(value);
        }

        public static float acos(float value)
        {
            return (float)Math.Acos(value);
        }

        public static float atan(float value)
        {
            return (float)Math.Atan(value);
        }

        public static float atan2(float valueA, float valueB)
        {
            return (float)Math.Atan2(valueA, valueB);
        }

        #endregion

        #region Vector Math

        //

        #endregion

        #region Matrix & Vector3

        // Generates a projection matrix for a draw call
        public static Matrix CreateProjectionMatrix(GraphicsDevice graphicsDevice)
        {
            return Matrix.CreatePerspectiveFieldOfView(MathHelper.PiOver4,
                (float)graphicsDevice.Viewport.Width
                / (float)graphicsDevice.Viewport.Height,
                .01f, 1000000);
        }

        // Creates a world matrix
        public static Matrix CreateWorldMatrix(Vector3 Translation)
        {
            return CreateWorldMatrix(Translation, Matrix.Identity);
        }

        // Creates a world matrix
        public static Matrix CreateWorldMatrix(Vector3 Translation,
            Matrix Rotation)
        {
            return CreateWorldMatrix(Translation, Rotation, Vector3.One);
        }

        // Creates a world matrix
        public static Matrix CreateWorldMatrix(Vector3 Translation,
            Matrix Rotation, Vector3 Scale)
        {
            return Matrix.CreateScale(Scale) *
                Rotation *
                Matrix.CreateTranslation(Translation);
        }

        // Converts a rotation vector into a rotation matrix
        public static Matrix Vector3ToMatrix(Vector3 Rotation)
        {
            return Matrix.CreateFromYawPitchRoll(Rotation.Y, Rotation.X, Rotation.Z);
        }

        // Converts a rotation matrix into a rotation vector
        public static Vector3 MatrixToVector3(Matrix Rotation)
        {
            Quaternion q = Quaternion.CreateFromRotationMatrix(Rotation);
            return new Vector3(q.X, q.Y, q.Z);
        }

        #endregion
    }
}
